using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using InfoExpress.HIV.WinApp.Utils;
using InfoExpress.WinApp.Utils;
using InfoExpress.HIV.Domains;
using InfoExpress.HIV.WinApp.Business;

namespace InfoExpress.HIV.WinApp.Forms
{
  public partial class FrmLichSuChuyenBenh : XtraForm
  {
    private TList<LichSuDieuTri> _lichSuDieuTriList = new TList<LichSuDieuTri>();
    private TList<UtDmdv> _donViList;
    private int _benhNhanId = 0;

    public FrmLichSuChuyenBenh()
    {
      InitializeComponent();
    }

    #region Event Handlers

    private void FrmLichSuChuyenBenh_Load(object sender, EventArgs e)
    {
      BindData();
    }

    #region Command Handlers

    private void btnView_Click(object sender, EventArgs e)
    {
      if (!string.IsNullOrEmpty(txtMaThe.Text))
      {
        if(Validate())
          LoadData();
      }
      else
        UIUtils.ShowInformationMessage(Constants.MsgInfo_001);
    }

    private void btnPrint_Click(object sender, EventArgs e)
    {

    }

    #endregion

    #region Data Loading

    private void bwDataLoading_DoWork(object sender, DoWorkEventArgs e)
    {
      try
      {
        _benhNhanId = BenhNhanUtils.LayBenhNhanId(txtMaThe.Text);
        _lichSuDieuTriList =
          GlobalCache.LichSuDieuTriService.GetByBenhNhanId(_benhNhanId);
        RefineData(_lichSuDieuTriList);
      }
      catch (Exception ex)
      {
        _lichSuDieuTriList = null;
        Logger.LogCritical("FrmLichSuChuyenBenh::bwDataLoading_DoWork", ex, "General", null);
      }
    }

    private void bwDataLoading_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
      Cursor = Cursors.Default;
      MainFormUtils.StopProgress();
      MainFormUtils.SetLoadedStatus();
      ToggleEditControls(true);

      if (_lichSuDieuTriList != null)
      {
        SetData();
        if (_lichSuDieuTriList.Count > 0)
        {
          gcLichSuChuyenBenh.DataSource = _lichSuDieuTriList;
        }
        else
        {
          gcLichSuChuyenBenh.DataSource = null;
          if (_benhNhanId == 0)
          {
            lblMaThe.Text = ".";
            lblTinhTrang.Text = ".";

            UIUtils.ShowInformationMessage(Constants.MsgInfoLsChuyenBenh_003);
          }
        }
      }
      else
      {
        UIUtils.ShowCannotLoadDataMessage();
      }
    }

    #endregion DataLoading BackgroundWorker

    #endregion

    #region Helper Methods

    private void BindData()
    {
      try
      {
        _donViList = GlobalCache.DonViService.GetAll();
        lueDonVi.Properties.DataSource = _donViList;
      }
      catch (Exception ex)
      {
        Logger.LogCritical("FrmLichSuChuyenBenh::BindData", ex, "General", null);
      }
    }

    private void LoadData()
    {
      Cursor = Cursors.WaitCursor;
      MainFormUtils.StartProgress();
      MainFormUtils.SetLoadingStatus();
      ToggleEditControls(false);
      bwDataLoading.RunWorkerAsync();
    }

    private void SetData()
    {
      lblMaThe.Text = txtMaThe.Text;
      lblTinhTrang.Text = (_lichSuDieuTriList.Count == 0) ?
        Constants.MsgInfoLsChuyenBenh_001 : Constants.MsgInfoLsChuyenBenh_002;

      if (_lichSuDieuTriList.Count > 0)
      {
        lblNgay1.Enabled = true;
        lblLblNgay1.Enabled = true;
        lblOPC1.Enabled = true;
        lblLblOpc1.Enabled = true;
        lblNgay2.Enabled = true;
        lblLblNgay2.Enabled = true;
        lblOPC2.Enabled = true;
        lblLblOpc2.Enabled = true;

        lblNgay1.Text =
          _lichSuDieuTriList[0].NgayBatDau.ToShortDateString();
        lblOPC1.Text = _donViList.Find("FMadv", _lichSuDieuTriList[0].MaOpc).FTendv;

        LichSuDieuTri lsDtCuoiCung = _lichSuDieuTriList[_lichSuDieuTriList.Count - 1];
        lblNgay2.Text =
          lsDtCuoiCung.NgayBatDau.ToShortDateString();
        lblOPC2.Text = _donViList.Find("FMadv", lsDtCuoiCung.MaOpc).FTendv;

        if (lsDtCuoiCung.NgayKetThuc.HasValue)
        {
          lblNgay3.Enabled = true;
          lblLblNgay3.Enabled = true;
          lblLyDo.Enabled = true;
          lblLblLyDo.Enabled = true;

          lblNgay3.Text =
            lsDtCuoiCung.NgayKetThuc.Value.ToShortDateString();
          lblLyDo.Text = lsDtCuoiCung.LyDo;
        }
      }
      else
      {
        lblNgay1.Text = ".";
        lblNgay1.Enabled = false;
        lblLblNgay1.Enabled = false;
        lblOPC1.Text = ".";
        lblOPC1.Enabled = false;
        lblLblOpc1.Enabled = false;
        lblNgay2.Text = ".";
        lblNgay2.Enabled = false;
        lblLblNgay2.Enabled = false;
        lblOPC2.Text = ".";
        lblOPC2.Enabled = false;
        lblLblOpc2.Enabled = false;
        lblNgay3.Text = ".";
        lblNgay3.Enabled = false;
        lblLblNgay2.Enabled = false;
        lblLyDo.Text = ".";
        lblLyDo.Enabled = false;
        lblLblLyDo.Enabled = false;
      }
    }

    private void ToggleEditControls(bool enabled)
    {
      btnView.Enabled = enabled;
      btnPrint.Enabled = enabled;
    }

    private void RefineData(TList<LichSuDieuTri> lichSuDieuTriList)
    {
      int count = lichSuDieuTriList.Count;
      for (int i = 0; i < count; i++)
      {
        LichSuDieuTri lichSuDieuTri = lichSuDieuTriList[i];
        if (lichSuDieuTri.MaKetThuc == (int)BenhNhanUtils.ChamDutDieuTri.ChuyenOpc
          && i < count - 1)
        {
          LichSuDieuTri lichSuDieuTriTiepTheo = lichSuDieuTriList[i + 1];
          lichSuDieuTri.TenOpc = _donViList.Find("FMadv", lichSuDieuTriTiepTheo.MaOpc).FTendv;
        }
        else
        {
          lichSuDieuTri.TenOpc = _donViList.Find("FMadv", lichSuDieuTri.MaOpc).FTendv;
        }
      }
    }

    #endregion

  }
}